//
//  GameViewController.m
//  BeatPlane
//
//  Created by LZXuan on 15-5-6.
//  Copyright (c) 2015年 轩哥. All rights reserved.
//

#import "GameViewController.h"
#import "Plane.h"


#define kScreenSize [UIScreen mainScreen].bounds.size

@interface GameViewController ()
{
    //游戏界面背景
    UIImageView *_backImageView;
    Plane *_plane;
    
    //定时器
    NSTimer *_timer;
    
    UIView *_towardsView;
    
}
@end

/*
 游戏界面 就是来管理游戏的
 管理 飞机 敌人 子弹
 
 */
@implementation GameViewController
- (void)dealloc {
    [_towardsView release];
    [_backImageView release];
    [super dealloc];
}

//代码入口
- (void)viewDidLoad {
    [super viewDidLoad];
    [self gameInit];
}

/*
 1.创建背景
 2.设计飞机
 3.设计方向按钮 控制飞机移动
 4.飞机发射子弹
 5.设计敌人 敌人移动
 6.碰撞检测
 7.其他
 
 */

#pragma mark - 游戏初始化
- (void)gameInit {
    //先创建背景
    [self creatGameBack];
    //创建飞机
    [self creatPlane];
    //创建方向
    [self creatTowardsView];
    
}
#pragma mark - 界面将要显示的时候
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    //创建定时器
    [self creatTimer];
}
//界面将要离开屏幕的时候调用
- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    if ([_timer isValid]) {//判断定时器是否有效
        [_timer invalidate];//终止定时器
        _timer = nil;
    }
}


#pragma mark - 定时器
//定时器 控制器 飞机的移动 敌人的移动 子弹移动 碰撞检测
- (void)creatTimer {
    if (_timer) {
        [_timer invalidate];//销毁
        _timer = nil;
    }
    _timer = [NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:@selector(timerClick) userInfo:nil repeats:YES];
}
//定时器 触发的方法
- (void)timerClick {
    //控制飞机的移动
    [_plane planeAction];
    
}

#pragma mark - 创建方向按钮
- (void)creatTowardsView {
    //方向视图
    _towardsView = [[UIView alloc] initWithFrame:CGRectMake(0, kScreenSize.height-50, 150, 50)];
    _towardsView.backgroundColor = [UIColor clearColor];
    //粘贴到画布
    [self.view addSubview:_towardsView];
    
    //创建方向按钮 把方向按钮粘到 _towardsView上
    NSArray *imageNames = @[@"button_left",@"button_right"];
    for (NSInteger i = 0; i < imageNames.count; i++) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(100*i, 0, 50, 50);
        //设置图片
        [button setBackgroundImage:[UIImage imageNamed:imageNames[i]] forState:UIControlStateNormal];
        button.tag = 301+i;//设置tag 值
        //增加事件
        //按下 改变飞机的方向
        [button addTarget:self action:@selector(changeToward:) forControlEvents:UIControlEventTouchDown];
        //抬起的时候 停止飞机移动
        [button addTarget:self action:@selector(stopPlane) forControlEvents:UIControlEventTouchUpInside];
        [button addTarget:self action:@selector(stopPlane) forControlEvents:UIControlEventTouchUpOutside];
        [_towardsView addSubview:button];
    }
}

#pragma mark - 方向按钮控制的方法
- (void)changeToward:(UIButton *)button {
    switch (button.tag) {
        case 301://左移
            //控制飞机方向
            [_plane startMoveLeft];
            break;
        case 302://右移
            //控制飞机方向
            [_plane startMoveRight];
            break;
            
        default:
            break;
    }
}
- (void)stopPlane {
    //停止移动
    [_plane stopMoveing];
}


#pragma mark - 创建飞机
- (void)creatPlane {
    _plane = [Plane defaultPlane];
    //粘贴到画布
    [self.view addSubview:_plane];
}

- (void)creatGameBack {
    _backImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, kScreenSize.width, kScreenSize.height)];
    _backImageView.image = [UIImage imageNamed:@"backgroud.png"];
    //粘贴到画布
    [self.view addSubview:_backImageView];
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
